package com.guaika.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guaika.reggie.common.BaseContext;
import com.guaika.reggie.common.ResultMessage;
import com.guaika.reggie.entity.AddressBook;
import com.guaika.reggie.mapper.AddressBookMapper;
import com.guaika.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GuaiKa
 */
@Slf4j
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Override
    public ResultMessage<AddressBook> addressSave(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.get());
        log.info("addressBook:{}", addressBook);
        this.save(addressBook);
        return ResultMessage.success(addressBook);
    }

    @Override
    public ResultMessage<AddressBook> addressSetDefault(AddressBook addressBook) {
        log.info("addressBook:{}", addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.get());
        wrapper.set(AddressBook::getIsDefault, 0);
        //SQL:update address_book set is_default = 0 where user_id = ?
        this.update(wrapper);

        addressBook.setIsDefault(1);
        //SQL:update address_book set is_default = 1 where id = ?
        this.updateById(addressBook);
        return ResultMessage.success(addressBook);
    }

    @Override
    public ResultMessage<?> addressGet(Long id) {
        AddressBook addressBook = this.getById(id);
        if (addressBook != null) {
            return ResultMessage.success(addressBook);
        } else {
            return ResultMessage.error("没有找到该对象");
        }
    }

    @Override
    public ResultMessage<AddressBook> addressGetDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.get());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = this.getOne(queryWrapper);

        if (null == addressBook) {
            return ResultMessage.error("没有找到该对象");
        } else {
            return ResultMessage.success(addressBook);
        }
    }

    @Override
    public ResultMessage<List<AddressBook>> addressList(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.get());
        log.info("addressBook:{}", addressBook);
        //条件-构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return ResultMessage.success(this.list(queryWrapper));
    }
}
